package com.dwy2002.common.utils;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Redis工具类
 *
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/03/23
 */
@Component
@RequiredArgsConstructor
public class RedisUtils {
    private final RedisTemplate<String, Object> redisTemplate;

    public <T> T get(String key, Class<T> clazz) {
        Object value = redisTemplate.opsForValue().get(key);
        return clazz.cast(value);
    }

    public <T> List<T> getList(String key, Class<T> clazz) {
        Object value = redisTemplate.opsForValue().get(key);
        if (value instanceof List<?> list) {
            return list.stream()
                    .map(clazz::cast)
                    .collect(Collectors.toList());
        }
        return null;
    }
}
